﻿using System;
using System.Web.Security;
using com.cruxomatic.controllers.Administration;
using com.cruxomatic.items.navigationstrip;
using com.cruxomatic.views;
using com.cruxomatic.controllers;
using com.cruxomatic.security;
using com.cruxomatic.configuration;

/// <summary>
/// Summary description for BaseView
/// </summary>
public class BaseView : System.Web.UI.Page, IBaseView
{
       public BaseView()
       {
           PreInit += BaseView_PreInit;
           ViewName = "";
       }

       public VistaAddressBar PageSiteMap
       {
           get
           {
               return UrlTreeNodeItems.GetSiteNavigationControl(Page);
           }
       }


    protected void BaseView_PreInit(object sender, EventArgs e)
     {
         Page.Theme = CruxConfigurationManager.GetApplicationThemeName();
         bool fixsessiontimeouterror = bool.Parse(CruxConfigurationManager.GetApplicationCustomSetting("FixSessionTimeoutError").Value);
           if (Context.Session != null && fixsessiontimeouterror)
           {
               if (Context.Session.IsNewSession)
               {
                   string returnUrl = Context.Response.RedirectLocation;
                   Response.Redirect(Page.ResolveUrl("~/login.aspx?ReturnUrl=" + returnUrl));
               }
           }

           ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
           SessionData sessionData = new SessionData
                                         {
                                             CurrentView = ViewName,
                                             CurrentViewSection = NavigationController.GetSectionName(ViewName)
                                         };

        //TODO: Remove in Production Or Deployment Based Enivornment
        //   sessionData.CurrentUser = cruxSecurityManager.GetUserFromCompany("crux@eforceglobal.com", "Some company");

        if (sessionData.CurrentUser == null || 
            !cruxSecurityManager.HasAccessToView(sessionData.CurrentUser.UserName,
               sessionData.CurrentUser.CompanyName, ViewName))
               FormsAuthentication.RedirectToLoginPage();

           if (!UsesPageData)
               sessionData.PageData = null;
           Page.Title = GetViewTitle(ViewName);
           
       }

    #region IBaseView Members

        public string ViewName { get; set; }

        string _ViewTitle = "";
        public string ViewTitle
        {
            get
            {
                if (string.IsNullOrEmpty(_ViewTitle))
                {
                    return GetViewTitle(ViewName);
                }
                return _ViewTitle;
            }
            set
            {
                _ViewTitle = LocalizationManager.GetLocalizedText(value);
            }
        }

        public bool UsesPageData { get; set; }

    #endregion

        private string GetViewTitle(string viewName)
        {
            
            string viewTitle = CruxConfigurationManager.GetViewTitle(viewName);
            return LocalizationManager.GetLocalizedText(viewTitle);
        }
}
